import os
import sys
import module002
import module003
import cekrun
import Cservice
import pywintypes

filename = os.path.split(sys.argv[0])
usage = """use : """ + filename[1] + """ [ kill | help | |gui | config ]"""
usage2 = """use : """ + filename[1] + """ [ start | stop | restart | status | kill | help | |gui | config ]"""

def main(data):
	data_app = os.path.split(data)	
	try:
		if (len(sys.argv) > 1):
			if(sys.argv[1] == 'kill'):
				os.system("taskkill /f /im " + data_app[1])
			elif(sys.argv[1] == 'help'):
				print "\n"
				print "\t", usage
			else:
				print "\n"
				print "\t", usage
		else:
			module002.main(data)
	except IndexError, e:
		print "\n"
		print "\t", usage
		
def services(data):
	try:
		if(len(sys.argv) > 1):
			if(sys.argv[1] == "stop"):
				for i in range (0,10):
					#try:
					Cservice.WService(data[i]).stop()
					print "\n"
					data_status = Cservice.WService(data[i]).status()
					print "\t Service " + data[i] + " is " + data_status
					#except pywintypes.error, e:
					#	pass
			elif(sys.argv[1] == "start"):
				for i in range (0, 10):
					Cservice.WService(data[i]).start()
					print "\n"
					data_status = Cservice.WService(data[i]).status()
					print "\t Service " + data[i] + " is " + data_status
			elif(sys.argv[1] == "restart"):
				for i in range (0, 10):
					Cservice.WService(data[i]).restart
					print "\n"
					data_status = Cservice.WService(data[i]).status()
					print "\t Service " + data[i] + " is " + data_status
			elif(sys.argv[1] == "status"):
				for i in range (0, 10):
					print "\n"
					data_status = Cservice.WService(data[i]).status()
					print "\t Service " + data[i] + " is " + data_status
			else:
				print "\n"
				print "\t" + usage2
		else:
			print "\n"
			print "\t" + usage2
					
	except IndexError, e:
		print "\n\n"
		pass
		#print "\t ERROR = ", str(e)
		
def main2(data,service):
	data_app = os.path.split(data)
	try:
		if (len(sys.argv) > 1):
			if(sys.argv[1] == 'kill'):
				os.system("taskkill /f /im " + data_app[1])
				module003.stop(service)
			elif(sys.argv[1] == 'help'):
				print "\n"
				print "\t", usage2
			elif(sys.argv[1] == "start" or "stop" or "restart" or "status"):
				if(sys.argv[1] == "restart"):
					os.system("taskkill /f /im " + data_app[1])
					module003.main(service)
				elif(sys.argv[1] == "start"):
					module002.main(data)
					module003.main(service)
				elif(sys.argv[1] == "stop"):
					os.system("taskkill /f /im " + data_app[1])
					module003.main(service)
				elif(sys.argv[1] == "status"):
					module003.main(service)
					cekrun.cek2(data_app[1].split(".exe")[0])
				else:
					pass
			else:
				print "\n"
				print "\t", usage2
		else:
			module003.start(service)
			module002.main(data)
	except IndexError, e:
		print "\n"
		print "\t", usage2
		
if __name__ == '__main__':
	try:
		main(sys.argv[1])
	except IndexError, e:
		pass
		